<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | gitee下载：https://gitee.com/likeshop_gitee
// | github下载：https://github.com/likeshop-github
// | 访问官网：https://www.likeshop.cn
// | 访问社区：https://home.likeshop.cn
// | 访问手册：http://doc.likeshop.cn
// | 微信公众号：likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用，未经许可不能去除前后端官方版权标识
// |  likeshop系列产品收费版本务必购买商业授权，购买去版权授权后，方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的，任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\admin\Logic;
use app\admin\model\BottleLog;
use app\common\server\UrlServer;
use think\Db;


class BottleLogLogic{
    public static function lists($get){
        $where = [];
        //类型搜索
        if (isset($get['bottle_code']) && $get['bottle_code']) {
            $where[] = ['b.bottle_code', 'like' , $get['bottle_code']."%"];
        }

        //启用状态搜索
        if (isset($get['type']) && $get['type']) {
            $where[] = ['b.type', '=' , $get['type']];
        }

        if (isset($get['order_id']) && $get['order_id']) {
            $where[] = ['b.order_id', '=' , $get['order_id']];
        }

        if (isset($get['admin_id']) && $get['admin_id']) {
            $where[] = ['b.admin_id', '=' , $get['admin_id']];
        }
        if (isset($get['remark']) && $get['remark']) {
            $where[] = ['b.remark', '=' , $get['remark']];
        }
        //启用日期
        if (isset($get['start_time']) && $get['start_time'] != '') {
            $where[] = ['b.create_time', '>=', $get['start_time']];
        }
        if (isset($get['end_time']) && $get['end_time'] != '') {
            $where[] = ['b.create_time', '<=', $get['end_time']];
        }
        $bottle = new BottleLog();

        $bottle_count = $bottle
            ->alias('b')
            ->where($where)
            ->leftJoin('driver d','b.admin_id=d.id')
            ->count();

        $bottle_list = $bottle
            ->alias('b')
            ->field('b.*,d.dname as admin_id_name')
            ->where($where)
            ->leftJoin('driver d','b.admin_id=d.id')
            ->page($get['page'], $get['limit'])
            ->order('id desc')
            ->select();
//        echo $bottle->getLastSql();
        return ['count' => $bottle_count, 'list' => $bottle_list];

    }
    //钢瓶记录创建-公共方法
    public static function createBottleLog($post){
        $bottle = new BottleLog();
        $data = $post;
        $data['create_time'] =date("Y-m-d H:i:s",time());
        $res = $bottle->create($data);
        return $res;
    }

    //添加钢瓶
    public static function add($post){
        $bottle = new BottleLog();
        //拼接数据
        $data = [
            'site_id'     => $post['site_id'],
            'code'        => $post['code'],
            'shared_code' => $post['shared_code'],
            'remark'      => $post['remark'],
            'status'      => 2,//钢瓶默认状态为空瓶
            'use_status'  => 1,//默认启用
            'createtime'  => date("Y-m-d H:i:s",time()),
        ];
        return $bottle->allowField(true)->save($data);
    }

    public static function edit($post){
        $bottle = new BottleLog();
        //拼接数据
//        $update_data = [
//            'id'          => $post['id'],
//            'site_id'     => $post['site_id'],
//            'code'        => $post['code'],
//            'shared_code' => $post['shared_code'],
//            'remark'      => $post['remark'],
//            'status'      => $post['status'],//钢瓶默认状态为空瓶
//            'use_status'  => $post['use_status'],//默认启用
//        ];
        $res = $bottle->save($post,['id'=>$post['id']]);
        if($res==false){
            return false;
        }
        return true;
    }

    public static function first($id,$get_data = false){
        $user =  BottleLog::get($id);
        return $user;
    }



    /*
     * 删除优惠券
     */
    public static function del($id){
        Db::startTrans();
        try{
            $coupon = new BottleLog();
            $coupon->save(['del'=>1,'update_time'=>time()],['id'=>$id]);

            Db::commit();
            return true;
        }catch(\Exception $e) {
            Db::rollback();
            return false;
        }
    }

}